const mysql = require('mysql')
// 加载mysql-queues 支持事务
var queues = require('mysql-queues')
// 加载async 支持顺序执行
var async = require('async')
let pool = null
function query(sql, values) {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) {
        console.error(sql, err)
      } else {
        connection.query(sql, values, (err, rows) => {
          if (err) {
            console.error(sql, err)
            reject(err)
          } else {
            connection.release()
            resolve(rows)
          }
        })
      }
    })
  })
}
exports.query = query
exports.init = function(co) {
  pool = mysql.createPool({
    host: co.HOST,
    user: co.USER,
    password: co.PSWD,
    database: co.DB,
    port: co.PORT,
    charset: 'utf8mb4',
    multipleStatements: true /* ,
    debug: ['ComQueryPacket'] */
  })
}
